<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once(SERVER_FS_ROOT."/app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");

$errMsg = sxREQ('msg');

$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
if(sxREQ("reqtype") == "load"){	
	$errMsg .= loadMb();
}
//output HTML
$aOpz = array("kMenu"=>'uti.cif', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function caricaMB() {
	document.objFrm.reqtype.value = 'load';
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"conf\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
//output comandi
$aCmd = array(
	"load"=>array("pag"=>"javascript:caricaMB()",
						"title"=>_t("compare file content with meta-base"),
						"text"=>_t("compare")
						),
	"back"=>array()
	);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\">Compare/Import</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">File : <input class='inpt01' type=\"file\" name=\"import_file\" size='80'></td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function loadMB () {
	if(!file_exists($_FILES["import_file"]["tmp_name"]) || $_FILES["import_file"]["size"] == 0){
		return _t("Upload FAILED!!");
	}
 	if (!is_dir(SERVER_FS_TEMP."/imex")) {
 		mkdir(SERVER_FS_TEMP."/imex",0775);
 	}
 	if ($dh = opendir(SERVER_FS_TEMP."/imex")) { //delete old file
 		$ymdLimit = date('ymd',time()-3600*48);
	  	while ($file = readdir($dh)) {
	      if (substr($file,0,3)=='exp' and substr($file,3,6)< $ymdLimit) {
	      	unlink(SERVER_FS_TEMP."/imex/".$file);
	      }
	   }
	   closedir($dh);
	}
 	if (!is_dir(SERVER_FS_TEMP."/imex")) {
 		return _t("Unable to create directory %s",SERVER_FS_TEMP."/imex");
 	}
 	$nomeFile = 'exp'.uniqid(date('ymd'));
	if(!copy($_FILES["import_file"]["tmp_name"], SERVER_FS_TEMP."/imex/$nomeFile.txt")) {
 		return _t("Unable to copy upload file in %s",SERVER_FS_TEMP."/imex/$nomeFile.txt");
	}
	serverRedir(SERVER_URL_ROOT."/app/util/imex/importMB.php?nf=$nomeFile&xsn=".XSN);
	return '???';
} 

?>
